//
//  MZViewController.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 02/01/13.
//  Copyright (c) 2013 Sitara Badourali. All rights reserved.
//

#import "MZViewController.h"

#define MZ_INFOS_VC_TITLE @"Infos"
#define MZ_YOUTUBE_VC_TITLE @"Youtube"
#define MZ_CONTACTS_VC_TITLE @"Contacts"
#define MZ_GALLERY_VC_TITLE @"Albums"
#define MZ_PROGRAMS_VC_TITLE @"Programme"
#define MZ_NEWS_VC_TITLE @"News"
#define MZ_TIMETABLE_VC_TITLE @"Horaires Namaz"
#define MZ_MENU_BACK_BUTTON_TITLE @"Accueil"

#import "Mehfile_ZainabViewController.h"
#import "MZInfosViewController.h"
#import "MZContactsViewController.h"
#import "MZGalleryViewController.h"
#import "MZProgramViewController.h"
#import "MZNewsViewController.h"
//#import "MZTimeTableViewController.h"
#import "MZTimeTableSegmentedViewController.h"
//#import "MZVideo.h"
#import "MZYoutubeViewController.h"

@interface MZViewController ()

@end

@implementation MZViewController

@synthesize infoType;
@synthesize centralButton;

-(IBAction)GoBack:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)displayYoutube {
    
    /*
    NSLog(@"Display Youtube web view from here");
    
	MZYoutubeViewController *mzYoutubeViewController = [[MZYoutubeViewController alloc] initWithNibName:@"MZYoutubeViewController" bundle:nil];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzYoutubeViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzYoutubeViewController.title = MZ_YOUTUBE_VC_TITLE;
    
    [self.navigationController pushViewController:mzYoutubeViewController animated:YES];
    
	[mzYoutubeViewController release];
	*/
    
    NSString *channelName = @"mehfilezainab";
    
    NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://user/%@",channelName]];
    NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/user/%@",channelName]];
    
    if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) {
        // Can open the youtube app URL so launch the youTube app with this URL
        [[UIApplication sharedApplication] openURL:linkToAppURL];
    }
    else{
        // Can't open the youtube app URL so launch Safari instead
        [[UIApplication sharedApplication] openURL:linkToWebURL];
    }
    
	return;
    
}

-(void)displayInfos {
	
	NSLog(@"Display Mehfile Zainab Infos from 1");
	
	MZInfosViewController *mzInfosViewController = [[MZInfosViewController alloc] initWithNibName:@"MZInfosViewController" bundle:nil];
    
    /*
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzInfosViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzInfosViewController.title = MZ_INFOS_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzInfosViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzInfosViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzInfosViewController.title = MZ_INFOS_VC_TITLE;
    
    [self.navigationController pushViewController:mzInfosViewController animated:YES];
    
	//[navController release];
	[mzInfosViewController release];
	
	return;
	
}

-(void)displayContacts {
	
	NSLog(@"Display Mehfile Zainab Contacts");
	
	MZContactsViewController *mzContactsViewController = [[MZContactsViewController alloc] initWithNibName:@"MZContactsViewController" bundle:nil];
	
    /*
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzContactsViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzContactsViewController.title = MZ_CONTACTS_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzContactsViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     
     [navController release];
     */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzContactsViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzContactsViewController.title = MZ_CONTACTS_VC_TITLE;
    
    [self.navigationController pushViewController:mzContactsViewController animated:YES];
    
	[mzContactsViewController release];
	
	return;
	
}

-(void)displayGallery {
	
	NSLog(@"Display Mehfile Zainab Gallery");
	
	MZGalleryViewController *mzGalleryViewController = [[MZGalleryViewController alloc] initWithNibName:@"MZGalleryViewController" bundle:nil];
	
    /*
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzGalleryViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzGalleryViewController.title = MZ_GALLERY_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzGalleryViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     
     [navController release];
     */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzGalleryViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzGalleryViewController.title = MZ_GALLERY_VC_TITLE;
    
    [self.navigationController pushViewController:mzGalleryViewController animated:YES];
    
	[mzGalleryViewController release];
	
	return;
	
}

-(void)displayProgram {
    
	NSLog(@"Display Mehfile Zainab Program");

    infoType = 2;
    
    /*
    NSLog(@"Remove Badge if existing");
    UIView *viewToRemove = [self.view viewWithTag:infoType];
    if (viewToRemove!=nil)
    {
        NSLog(@"Badge not nil, removing it!");
        //[viewToRemove performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
        [viewToRemove removeFromSuperview];
        //[viewToRemove release];
        viewToRemove = nil;
    }
    */
    
    for (UIView *subview in [self.view subviews]) {
        
        if (subview.tag == infoType){
            
            [subview removeFromSuperview];
            
        }
    }
    
	MZProgramViewController *mzProgramViewController = [[MZProgramViewController alloc] initWithNibName:@"MZProgramViewController" bundle:nil];
	
    /*
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzProgramViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzProgramViewController.title = MZ_PROGRAMS_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzProgramViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     
     [navController release];
     */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzProgramViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzProgramViewController.title = MZ_PROGRAMS_VC_TITLE;
    
    [self.navigationController pushViewController:mzProgramViewController animated:YES];
    
	[mzProgramViewController release];
	
	return;
	
}

-(void)displayNews {
    
	NSLog(@"Display Mehfile Zainab News");
    
    infoType = 1;
    
    NSLog(@"Remove Badge if existing");
    
    /*
    UIView *viewToRemove = [self.view viewWithTag:infoType];
    if (viewToRemove!=nil)
    {
        NSLog(@"Badge not nil, removing it!");
        //[viewToRemove performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
        [viewToRemove removeFromSuperview];
        //[viewToRemove release];
        viewToRemove = nil;
    }
    */
    
    for (UIView *subview in [self.view subviews]) {
        
        if (subview.tag == infoType){
            
            [subview removeFromSuperview];
            
        }
    }
    
	MZNewsViewController *mzNewsViewController = [[MZNewsViewController alloc] initWithNibName:@"MZNewsViewController" bundle:nil];
	
    /*
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzNewsViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzNewsViewController.title = MZ_NEWS_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzNewsViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     
     [navController release];
     */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzNewsViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzNewsViewController.title = MZ_NEWS_VC_TITLE;
    
    [self.navigationController pushViewController:mzNewsViewController animated:YES];
    
	[mzNewsViewController release];
	
	return;
	
}

-(void)displayTimeTable {
	
	NSLog(@"Display Mehfile Zainab Time Table");
	
    /*
     MZTimeTableViewController *mzTimeTableViewController = [[MZTimeTableViewController alloc] initWithNibName:@"MZTimeTableViewController" bundle:nil];
     
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzTimeTableViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzTimeTableViewController.title = MZ_TIMETABLE_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzTimeTableViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     
     [navController release];
     [mzTimeTableViewController release];
     */
    
    MZTimeTableSegmentedViewController *mzTimeTableViewController = [[MZTimeTableSegmentedViewController alloc] initWithNibName:@"MZTimeTableSegmentedViewController" bundle:nil];
    
    /*
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mzTimeTableViewController];
     navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
     mzTimeTableViewController.title = MZ_TIMETABLE_VC_TITLE;
     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
     style:UIBarButtonItemStyleBordered
     target:self
     action:@selector(GoBack:)];
     mzTimeTableViewController.navigationItem.leftBarButtonItem = barButtonItem;
     [self presentModalViewController:navController animated:YES];
     
     [navController release];
     */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:MZ_MENU_BACK_BUTTON_TITLE
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    mzTimeTableViewController.navigationItem.rightBarButtonItem = barButtonItem;
    
    mzTimeTableViewController.title = MZ_TIMETABLE_VC_TITLE;
    
    [self.navigationController pushViewController:mzTimeTableViewController animated:YES];
    
	[mzTimeTableViewController release];
    
	return;
	
}

-(void) addButton:(NSString*)imageName inFrame:(CGRect)frameRect forSelector:(SEL)selector
{
    UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    button.frame = frameRect;
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[[UIImage imageNamed:imageName] scaleToSize:CGSizeMake(button.frame.size.width, button.frame.size.width) ] forState:UIControlStateNormal];
    [button setBackgroundImage:[[UIImage imageNamed:imageName] scaleToSize:CGSizeMake(button.frame.size.width, button.frame.size.width) ] forState:UIControlStateHighlighted];
    [self.view addSubview:button];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        /*
        self.navigationController.navigationBar.translucent = YES;
        self.wantsFullScreenLayout = YES;
        UIImageView *bckGroundImage = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"Home-retina.png"] scaleToSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)]];
        [self.view addSubview:bckGroundImage];
        [self.view sendSubviewToBack:bckGroundImage];
        [bckGroundImage release];
        
        UINavigationBar *navBar = [self.navigationController navigationBar];
        UIImage *backgroundImage = [UIImage imageNamed:@"Home_0000_navigation-bar.png"];
        //[backgroundImage drawInRect:CGRectMake(0, 0, navBar.frame.size.width, navBar.frame.size.height)];
        [navBar setBackgroundImage:[backgroundImage scaleToSize:CGSizeMake(navBar.frame.size.width, navBar.frame.size.height)] forBarMetrics:UIBarMetricsDefault];
        
        [self addButton:@"Home_0005s_0009_Informations" inFrame:CGRectMake(15, 180, 90, 65) forSelector:@selector(displayInfos)];
        [self addButton:@"Home_0005s_0008_Programme" inFrame:CGRectMake(115, 115, 90, 65) forSelector:@selector(displayProgram)];
        [self addButton:@"Home_0005s_0000_News" inFrame:CGRectMake(235, 180, 50, 65) forSelector:@selector(displayNews)];
        [self addButton:@"Home_0005s_0004_Albums" inFrame:CGRectMake(230, 295, 60, 65) forSelector:@selector(displayGallery)];
        [self addButton:@"Home_0005s_0002_Contact" inFrame:CGRectMake(130, 355, 60, 55) forSelector:@selector(displayContacts)];
        [self addButton:@"Home_0005s_0008_Horaires-Namaaz" inFrame:CGRectMake(30, 295, 60, 65) forSelector:@selector(displayTimeTable)];
        */
        
    }
    return self;
}

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
    NSLog(@"Bouton central utilisé");
    [self displayInfos];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"View did load for MZViewController");
    
	// Temporary disabled
	// Feature to enable for future release
	/*
    self.centralButton.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    tapRecognizer.numberOfTapsRequired = 1;
    [self.centralButton addGestureRecognizer:tapRecognizer];
    */
	
}

- (void)viewDidAppear:(BOOL)animated {
	
	// GAI : feature to enable for future release
	//self.screenName = @"Vue principale";
	
	[super viewDidAppear:animated];

}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:YES];
    
    NSLog(@"Main menu view will disappear");

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
